import SnowflakeId from "snowflake-id";
const CourseVideoModels = require('../models/course_video');

interface courseVideo {
    id?: number
    course_id?: number,
    name: string,
    video: string
}

class CourseVideoService {
    // 获取课程视频列表
    public async getVideos() {
        const res: any = await CourseVideoModels.findAndCountAll()
        return res ? res : null
    }
    // 分页获取课程视频列表
    public async getVideosByPage(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await CourseVideoModels.findAndCountAll({
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据id获取课程视频
    public async getVideoById(id: number) {
        const res = await CourseVideoModels.findOne({
            where: {
                id
            }
        })
        return res? res.dataValues : null
    }
    // 添加课程视频
    public async createCourseVideo(params: courseVideo) {
        const id = new SnowflakeId();
        const temp = id.generate() + ''
        params.id = +(temp.slice(temp.length - 9));
        const res: any = await CourseVideoModels.create(params)
        return res ? res.dataValues : null;
    }
    // 更新课程视频
    public async update(id: number, params: courseVideo) {
        const whereOpt = { id }
        const res = await CourseVideoModels.update(params, { where: whereOpt })
        return res[0] > 0
    }
    // 删除课程视频
    public async deleteVideo(id: number) {
        const res: any = await CourseVideoModels.destroy({
            where: { id }
        })
        return res > 0
    }
}

module.exports = new CourseVideoService()
// 解决同目录下多个ts文件都引入Op,会报错的问题
export { }